﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace dataSendModel.Util
{
    public class logger
    {
        private string dir = "dataSend_Log";
        private string dirGps = "gps";
        private string dirReader = "reader";
        private string dirInfo = "info";
        private string rootPath = string.Empty;
        private StreamWriter infoSw;

        public StreamWriter InfoSw
        {
            get { return infoSw; }
            set { infoSw = value; }
        }
        private StreamWriter gpsSw;

        public StreamWriter GpsSw
        {
            get { return gpsSw; }
            set { gpsSw = value; }
        }
        private StreamWriter readerSw;

        public StreamWriter ReaderSw
        {
            get { return readerSw; }
            set { readerSw = value; }
        }

        public logger()
        {	
            rootPath = Directory.GetCurrentDirectory();
            rootPath = rootPath.Substring(0, 2);
            //rootPath = "E:";
            if (!Directory.Exists(rootPath + "\\" + dir))
            {
                DirectoryInfo di = Directory.CreateDirectory(rootPath + "\\" + dir);
                di.CreateSubdirectory(dirGps);
                di.CreateSubdirectory(dirReader);
                di.CreateSubdirectory(dirInfo);
            }
            string nowTime = DateTime.Now.ToString("d");
            gpsSw = File.AppendText(rootPath+"\\"+dir + "\\" + dirGps + "\\gps" + nowTime + ".txt");
            readerSw = File.AppendText(rootPath+"\\"+dir + "\\" + dirReader + "\\reader" + nowTime + ".txt");
            infoSw = File.AppendText(rootPath+"\\"+dir + "\\" + dirInfo + "\\info" + nowTime + ".txt");
        }

        public void gpsWrite(string msg)
        {
            gpsSw.WriteLine(msg);
            gpsSw.Flush();
        }

        public void readerWrite(string msg)
        {
            readerSw.WriteLine(msg);
            readerSw.Flush();
        }

        public void infoWrite(string msg)
        {
            infoSw.WriteLine(msg);
            infoSw.Flush();
        }
    }
}
